package smartcampus.scjava.controller.second;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartcampus.scjava.POJO.DO.AcademicArchives;

import smartcampus.scjava.POJO.DTO.AcademicArchivesDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.mapper.StudentMapper;
import smartcampus.scjava.service.AcademicArchivesService;
import smartcampus.scjava.service.StudentService;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

@RequestMapping("/archeives")
@ApiOperation("学籍表")
@RestController
public class AcademicArcheivesController {
    @Autowired
    AcademicArchivesService academicArcheivesService;

    @Autowired
    StudentService studentService;

    @Resource
    StudentMapper studentMapper;

    /***
     * 新增一条学籍档案信息
     * @param academicArchives
     * @return
     */
    @PostMapping("/insertAcademicArchives")
    @ApiOperation("插入一条学籍档案信息")
    public ResultVO<?> insertAcademicArchives(@RequestBody AcademicArchives academicArchives) {
        //1,先检查输入的学号在个人信息表中是否存在
        Boolean stuid = studentService.selectByStuid(academicArchives.getStuid());
        //2.如果不存在则返回请求失败
        if (stuid) {
            //查看该学号是否已经存在学籍档案管理表
            String stuid11 = selectStuid(academicArchives.getStuid());
            if (null == stuid11) {
                //没有数据，则执行添加函数
                Date now1 = new Date();
                academicArchives.setCreateTime(now1);
                Boolean a = academicArcheivesService.addArcheives(academicArchives);
                if (null != a) {
                    return ResultVO.success("添加成功");
                } else {
                    return ResultVO.fail("添加失败");
                }
            } else {
                return ResultVO.fail("该学号已存在，请重新添加");
            }
        } else {
            return ResultVO.fail("学生个人信息表中未查询到该学号，请在学生表中添加学号信息");
        }

    }

    @PostMapping("/delBystuId")
    @ApiOperation("根据学号删除一条信息")
    public ResultVO<?> delByStuid(@RequestBody AcademicArchives academicArchives) {

        Boolean aBoolean = academicArcheivesService.delByStuId(academicArchives.getStuid());
        if (!aBoolean) {
            return ResultVO.fail("删除失败,学籍档案中未找到该学号");
        } else {
            return ResultVO.success("删除成功");
        }

    }

    @PostMapping("/updateByStuId")
    @ApiOperation("根据学号修改一条信息")
    public ResultVO<?> updateByStuId(@RequestBody AcademicArchives academicArchives) {
        boolean aBoolean = academicArcheivesService.updateByStuId(academicArchives);
        if (!aBoolean) {
            return ResultVO.fail("修改失败");
        } else {
            return ResultVO.success("修改成功");
        }
    }


    @GetMapping("/selectBystuId")
    @ApiOperation("根据学号查询所有信息")
    public ResultVO<?> selectBystuId(AcademicArchivesDTO academicArchives) {
        AcademicArchivesDTO a = academicArcheivesService.selectByStuId(academicArchives.getStuid());
        if (null != a) {
            return ResultVO.success(a);
        } else {
            return ResultVO.fail("未查询到数据");
        }
    }

    @GetMapping("/selectAllList")
    @ApiOperation("分页查询所有信息")
    public ResultVO<?> selectAllList(@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        //利用pagehelper插件获取页面信息pagenum=第几页，pagesize=每页大小
        PageHelper.startPage(pageNum, pageSize);
        List<AcademicArchivesDTO> list = academicArcheivesService.findAll();
        PageInfo<AcademicArchivesDTO> pageInfo = new PageInfo<AcademicArchivesDTO>(list);

//        PageHelper.startPage(pagenum,pagesize).doSelectPage(()->studentMapper.se)
        return ResultVO.success(pageInfo);
    }


    @GetMapping("/selectOneByStuid")
    @ApiOperation("根据学号查询一条详情信息")
    public ResultVO<?> selectOneByStuid(String stuid) {
        QueryWrapper<AcademicArchives> qw = new QueryWrapper<AcademicArchives>();
        qw.eq("stuId", stuid);
        AcademicArchives student1 = academicArcheivesService.getOne(qw);
        if (null != student1) {
            return ResultVO.success(student1);
        }else {
            return ResultVO.fail("未查询到学号");
        }
    }

    public String selectStuid(String stuid) {
        QueryWrapper<AcademicArchives> qw = new QueryWrapper<AcademicArchives>();
        qw.eq("stuId", stuid);
        AcademicArchives student1 = academicArcheivesService.getOne(qw);
        if (null != student1) {
            return stuid;
        } else {
            return null;
        }
    }


}
